﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using JumbotPay.API.YunGouOS.WxPay;

namespace JumbotPay.WebFile.YungGouOS_Wxpay_Mobile
{
    public partial class _default_step3 : JumbotPay.UI.BasicPage
    {
        public static string wxJsApiParam { get; set; } //H5调起JS API参数
        public string account_wxpay;
        public string Account_Wxpay_Name;
        public string Order_ID = "";
        public string Product_Name = "";
        public string Product_Desc = "";
        public string Order_Time = "";
        public string Order_Money = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string _openid = q("openid");
            Order_ID = q("order_id");
            account_wxpay = q("account_no");
            WxPayData req = new WxPayData();
            object[] _value = new JumbotPay.DAL.OrderDAL().GetOrderFields("yungouos_wxpay", Order_ID, "", "product_name,product_desc,pay_amount,Order_Time,account_wxpay_name");
            Product_Name = _value[0].ToString();                            //商品名称
            Product_Desc = _value[1].ToString();                                 //商品描述
            double total_fee =  Convert.ToDouble(_value[2].ToString());
            Order_Time = Convert.ToDateTime(_value[3].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            Account_Wxpay_Name = _value[4].ToString();
            Order_Money = total_fee.ToString("f2");
            string _out_trade_no = new WxPayApi().GenerateOutTradeNo(account_wxpay);
            _out_trade_no = new JumbotPay.DAL.OrderDAL().UpdateOutOrderNO(Order_ID, _out_trade_no);
            try
            {
                JsApiPay jsApiPay = new JsApiPay(this);

                //JSAPI支付预处理
                //统一下单
                WxPayData data = new WxPayData();
                data.SetValue("out_trade_no", _out_trade_no, true);
                data.SetValue("total_fee", total_fee == 88888 ? 0.01 : total_fee, true);//分
                data.SetValue("body", Product_Desc, true);
                data.SetValue("openId", _openid, true);
                data.SetValue("attach", Order_ID);
                data.SetValue("return_url", site.Url + "/yungouos_wxpay_mobile/return_url.aspx?account_no=" + account_wxpay);
                try
                {
                    string unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(data, account_wxpay);
                    JumbotPay.Utils.Logs.Info("wxpayM_unifiedOrder_Result", "" + unifiedOrderResult, false);
                    Newtonsoft.Json.Linq.JObject jo = Newtonsoft.Json.Linq.JObject.Parse(unifiedOrderResult);
                    string _code = jo["code"].ToString();
                    if (_code == "0")
                    {
                        try
                        {

                            wxJsApiParam = jsApiPay.GetJsApiParameters(jo["data"].ToString());//获取H5调起JS API参数                    
                            JumbotPay.Utils.Logs.Info("wxpayM_GetJsApiParameters", "" + wxJsApiParam, false);

                        }
                        catch (Exception ex)
                        {
                            Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败，请返回重试" + "</span>");
                            JumbotPay.Utils.Logs.Error("wxpay_GetJsApiParameters", jo["data"].ToString() + "\r\n" + ex, false);
                        }
                    }
                    else
                    {
                        Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败，请返回重试" + "</span>");
                        JumbotPay.Utils.Logs.Error("wxpay_GetUnifiedOrder_jsApiPay", unifiedOrderResult, false);
                    }

                }
                catch (Exception ex)
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败，请返回重试" + "</span>");
                    JumbotPay.Utils.Logs.Error("wxpay_GetUnifiedOrderResult", data.ToJson() + "\r\n" + ex, false);
                }
            }
            catch (Exception ex)
            {
                JumbotPay.Utils.Logs.Error("wxpay_GetEditAddressParameters", "" + ex, true);
                Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错，请重试" + "</span>");
            }
            
        }
    }
}
